﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace School.Accounting
{
    static class MoneyToString
    {
        //مصفوفات الكلمات
        static string[] ahad = { "", "واحد", "إثنين", "ثلاثة", "أربعة", "خمسة", "ستة", "سبعة", " ثمانية", " تسعة", " عشرة", " أحد", " اثنى" };
        static string[] ahad2 = { "", "واحد", "إثنين", "ثلاثة", "أربعة", "خمسة", "ستة", "سبعة", "ثمانية", "تسعة", " عشر", " أحد", " اثنى" };
        static string[] asharat = { "", "واحد", "عشرون", "ثلاثون", "أربعون", "خمسون", "ستون", "سبعون", "ثمانون", "تسعون" };
        static string[] meat = { "", "مائة", "مائتين", "ثلاثمائة", "أربعمائة", "خمسمائة", "ستمائة", "سبعمائة", "ثمانمائة", "تسعمائة" };
        static string[] melion = { "", " مليون", " مليونان", " ملايين" };
        static string[] alf = { "", " ألف", " ألفين", " آلاف" };
        static string[] bcur = { " جنيه", " جنيهان", " جنيهات" };

        static public string NumToStr(double P_Num)
        {
            double rv;
            string accum = "";
            //الملايين
            rv = (int)(P_Num / 1000000);

            if (rv > 2)
                accum = NumToStr1(rv, accum);

            if (rv >= 3 && rv < 10)
                accum = accum + melion[3];
            else if (rv == 2)
                accum = accum + melion[2];
            else if ((rv == 1) || (rv >= 10 && rv <= 999))
                accum = accum + melion[1];
            //الآلاف
            rv = P_Num - (int)(P_Num / 1000000) * 1000000;
            rv = (int)(rv / 1000);
            if ((P_Num != ((int)(P_Num / 1000000)) * 1000000) && (P_Num > 1000000))
                accum = accum + " و";
            if (rv > 2)
                accum = NumToStr1(rv, accum);
            if (rv >= 3 && rv < 10)
                accum = accum + alf[3];
            else if (rv == 2)
                accum = accum + alf[2];
            else if ((rv == 1) || (rv >= 10 && rv <= 999))
                accum = accum + alf[1];
            //الباقي
            rv = P_Num - ((int)(P_Num / 1000)) * 1000;
            rv = (int)(rv + 0.0001);

            if ((P_Num != ((int)(P_Num / 1000)) * 1000) && (P_Num > 1000) && (rv != 0))
                accum = accum + " و ";

            if ((rv >= 2) && (P_Num != 2))
                accum = NumToStr1(rv, accum);

            if (P_Num > 0.999)
            {
                if ((P_Num < 11) && (rv > 2))
                    accum = accum + bcur[2];
                else if (P_Num == 2)
                    accum = accum + bcur[1];
                else
                    accum = accum + bcur[0];
            }

            //الهللات
            rv = P_Num - ((int)(P_Num + 0.0001)) + 0.0001;
            rv = (int)(rv * 1000);
            rv = rv / 10;

            if ((rv >= 1) && (P_Num > 0.99))
                accum = accum + " و";

            if (rv > 2.9)
                accum = NumToStr1(rv, accum);

            if (rv >= 1)
            {
                if ((rv == 2))
                    accum = accum + " قرشين";
                else if ((rv < 11) && (rv > 2.9))
                    accum = accum + " قروش";
                else
                    accum = accum + " قرش";
            }
            return accum;
        }

        //******************* NumToStr1 *************************
        // used by NmToStr
        static public string NumToStr1(double rv, string accum)
        {
            int b, c;
            if (rv >= 100)
            {
                b = (int)(rv / 100);
                accum = accum + meat[b];
            }

            b = (int)(rv - ((int)(rv / 100) * 100));
            if ((b != 0) && (rv > 99))
                accum = accum + " و";

            c = b - ((int)(b / 10) * 10);
            if ((b < 13) && (b != 0))
                accum = accum + ahad[b];

            if ((b > 12) && (c != 0))
                accum = accum + ahad2[c];
            if ((b > 10) && (b < 20))
                accum = accum + ahad2[10];

            if (b > 19)
            {
                if (c != 0)
                    accum = accum + " و";
                accum = accum + asharat[b / 10];
            }
            return accum;
        }
    }
}
